package com.google.android.exoplayer2.offline;

import android.net.Uri;
import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.util.AtomicFile;
import com.google.android.exoplayer2.util.Util;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

@Deprecated
/* loaded from: classes.dex */
final class ActionFile {
    private static final int VERSION = 0;
    private final AtomicFile atomicFile;

    public ActionFile(File file) {
        this.atomicFile = new AtomicFile(file);
    }

    public boolean exists() {
        return this.atomicFile.exists();
    }

    public void delete() {
        this.atomicFile.delete();
    }

    public DownloadRequest[] load() throws IOException {
        if (exists()) {
            try {
                InputStream openRead = this.atomicFile.openRead();
                DataInputStream dataInputStream = new DataInputStream(openRead);
                int readInt = dataInputStream.readInt();
                if (readInt > 0) {
                    throw new IOException("Unsupported action file version: " + readInt);
                }
                int readInt2 = dataInputStream.readInt();
                ArrayList arrayList = new ArrayList();
                for (int i = 0; i < readInt2; i++) {
                    try {
                        arrayList.add(readDownloadRequest(dataInputStream));
                    } catch (DownloadRequest.UnsupportedRequestException unused) {
                    }
                }
                DownloadRequest[] downloadRequestArr = (DownloadRequest[]) arrayList.toArray(new DownloadRequest[0]);
                Util.closeQuietly(openRead);
                return downloadRequestArr;
            } catch (Throwable th) {
                Util.closeQuietly((Closeable) null);
                throw th;
            }
        }
        return new DownloadRequest[0];
    }

    private static DownloadRequest readDownloadRequest(DataInputStream dataInputStream) throws IOException {
        byte[] bArr;
        String readUTF = dataInputStream.readUTF();
        int readInt = dataInputStream.readInt();
        Uri parse = Uri.parse(dataInputStream.readUTF());
        boolean readBoolean = dataInputStream.readBoolean();
        int readInt2 = dataInputStream.readInt();
        String str = null;
        if (readInt2 != 0) {
            byte[] bArr2 = new byte[readInt2];
            dataInputStream.readFully(bArr2);
            bArr = bArr2;
        } else {
            bArr = null;
        }
        boolean z = true;
        boolean z2 = readInt == 0 && DownloadRequest.TYPE_PROGRESSIVE.equals(readUTF);
        ArrayList arrayList = new ArrayList();
        if (!z2) {
            int readInt3 = dataInputStream.readInt();
            for (int i = 0; i < readInt3; i++) {
                arrayList.add(readKey(readUTF, readInt, dataInputStream));
            }
        }
        if (readInt >= 2 || (!DownloadRequest.TYPE_DASH.equals(readUTF) && !DownloadRequest.TYPE_HLS.equals(readUTF) && !DownloadRequest.TYPE_SS.equals(readUTF))) {
            z = false;
        }
        if (!z && dataInputStream.readBoolean()) {
            str = dataInputStream.readUTF();
        }
        String generateDownloadId = readInt < 3 ? generateDownloadId(parse, str) : dataInputStream.readUTF();
        if (readBoolean) {
            throw new DownloadRequest.UnsupportedRequestException();
        }
        return new DownloadRequest(generateDownloadId, readUTF, parse, arrayList, str, bArr);
    }

    private static StreamKey readKey(String str, int i, DataInputStream dataInputStream) throws IOException {
        int i2;
        int readInt;
        int readInt2;
        if ((DownloadRequest.TYPE_HLS.equals(str) || DownloadRequest.TYPE_SS.equals(str)) && i == 0) {
            i2 = 0;
            readInt = dataInputStream.readInt();
            readInt2 = dataInputStream.readInt();
        } else {
            i2 = dataInputStream.readInt();
            readInt = dataInputStream.readInt();
            readInt2 = dataInputStream.readInt();
        }
        return new StreamKey(i2, readInt, readInt2);
    }

    private static String generateDownloadId(Uri uri, String str) {
        return str != null ? str : uri.toString();
    }
}
